package com.itbaima.controller;

import com.itbaima.pojo.VO.SalaryVO;
import com.itbaima.result.Result;
import com.itbaima.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/salaries")  // 确保有正确的路径
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    @GetMapping("/")
    public Result<List<SalaryVO>> getAllSalaries() {
        List<SalaryVO> salaryList = salaryService.getAllSalaries();
        return Result.success(salaryList);
    }

    /**
     * 更新薪酬信息中的奖励金和扣除金
     * @param salaryId 薪酬标准ID
     * @param bonus 奖励金
     * @param dock 扣除金
     * @return 更新结果
     */
    @PostMapping("/update")
    public ResponseEntity<String> updateSalary(
            @RequestParam Integer salaryId,
            @RequestParam Double bonus,
            @RequestParam Double dock) {
        try {
            boolean isUpdated = salaryService.updateSalary(salaryId, bonus, dock);
            if (isUpdated) {
                return ResponseEntity.ok("薪酬信息更新成功！");
            } else {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("薪酬信息更新失败！");
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器错误：" + e.getMessage());
        }
    }
}

